#include "stdafx.h"
#include "GuiController.h"
#include <IGUIWindow.h>
#include <IGUIImage.h>
#include <GameLogic/DieNumber.h>
#include <common/StringUtils.h>
#include <GameLogic/BoardLogic.h>
#include "IGUIStaticText.h"

namespace catan
{
	class GuiController : public IEventReceiver, public IGuiController
	{
	public:
		GuiController(void){mInited = false;}
		~GuiController(void){};

		virtual void SetPlayerTrun() 
		{
			gui::IGUIElement* btn = mHelper->GetGUIElement(WinId::DieReqBtn);
			btn->setEnabled(true);
		}

		void OnDieReqBtnClicked()
		{
			gui::IGUIElement* btn = mHelper->GetGUIElement(WinId::DieReqBtn);
			btn->setEnabled(false);
			mCallback->OnDieReq();
			
		}

		virtual void ShowDie(DieNumber& dieNumber)
		{
			gui::IGUIImage* image1 = (gui::IGUIImage*)mHelper->GetGUIElement(WinId::Die1);
			VALID(image1);
			irr::video::ITexture* dieTex1 = mHelper->GetDevice()->getVideoDriver()->getTexture(tstring(TexDirectory() + _T("die") + ToString(dieNumber.mFirst) + _T(".png")).c_str());
			VALID(dieTex1);
			image1->setImage(dieTex1);
			gui::IGUIImage* image2 = (gui::IGUIImage*)mHelper->GetGUIElement(WinId::Die2);
			VALID(image2);
			irr::video::ITexture* dieTex2 = mHelper->GetDevice()->getVideoDriver()->getTexture(tstring(TexDirectory() + _T("die") + ToString(dieNumber.mSecond) + _T(".png")).c_str());
			VALID(dieTex2);
			image2->setImage(dieTex2);

		}

		virtual void UpdateResourceCard()
		{
			gui::IGUIStaticText* text = (gui::IGUIStaticText*)mHelper->GetGUIElement(WinId::CardNum);
			VALID(text);

			Handle<Player> p = mHelper->GetBoardLogic()->GetPlayer(mHelper->GetHostColor());
			VALID(p);
			int resourceCardNum[ResourceType::Num] = {0};
			for (ResourceCards::const_iterator it(p->mResourceCards.begin());
				it != p->mResourceCards.end(); ++it)
			{
				++resourceCardNum[(*it)->Type()];
			}
			
			tstring show;
			for (int i = ResourceType::Start; i < ResourceType::Num; ++i)
			{
				show += FieldResourceType::ToEngString((FieldResourceType::Enum)i) + _T(": ") + ToString(resourceCardNum[i]);
			}
			text->setText(show.c_str());

		}

		void Create()
		{

		}
		void Display()
		{
			if (mInited == false)
			{
				Init();
				mInited = true;
			}
			gui::IGUIEnvironment* gui = mHelper->GetDevice()->getGUIEnvironment();
			gui->drawAll();
		}
		virtual bool OnEvent(const SEvent& event)
		{
			switch(event.EventType)
			{
			case irr::EET_GUI_EVENT:
				{
					switch (event.GUIEvent.EventType )
					{
					case gui::EGET_BUTTON_CLICKED: 
						{
							switch(event.GUIEvent.Caller->getID())
							{
							case WinId::DieReqBtn: OnDieReqBtnClicked(); return true;
							};
						}
					}
				}
				
			}
			return true;
		}


		bool Init()
		{
			Handle<Board> board = mHelper->GetBoard();
			gui::IGUIEnvironment* gui = mHelper->GetDevice()->getGUIEnvironment();
			gui->setUserEventReceiver(this);
			if (!gui->loadGUI(_T("E:\\prj\\GamePrj\\catan\\bin\\res\\xml\\gui.xml")))
			{
				LogError(_T("load gui failed"));
				return false;
			}
			mHelper->GetGUIElement(WinId::DieReqBtn)->setEnabled(false);
			//UIConfig config(_T("./res/uiset.ini"));

// 			gui::IGUIWindow* win = gui->addWindow(config.GetRect(WinId::ToString(WinId::DieWindow), _T("rect")), false, _T("DIEWINDOW"), 0, WinId::DieWindow);
// 			win->setDrawTitlebar(false);
// 			win->set
// 			win->setDrawBackground(true);
// 			gui->addSpinBox(_T("aadsa"), core::recti(0,0,50,50), true, win);
			return true;
		}
		bool mInited;
	};

	IGuiController* IGuiController::Create()
	{
		return new GuiController();
	}
}